#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define ERROR_CHECK(ret, num, msg) \
    {                              \
        if (ret == num)            \
        {                          \
            perror(msg);           \
            exit(1);               \
        }                          \
    }
int main(int argc, char *argv[])
{
    int fd = open(argv[1], O_RDONLY);
    ERROR_CHECK(-1, fd, "open");
    int line = 1;
    char *p = "\t \n";
    char *word = NULL;
    char *lineBuf = (char *)malloc(32 *sizeof(char));
    char ch = '\0';
    int lenline = 0;
    while (read(fd, &ch, 1))
    {
       

        if (ch == '\n')
        {
            lineBuf[lenline] = '\0';
            word = strtok(lineBuf, p);
            
            while (word)
            {
                printf("%s--%s",word,argv[2]);
                printf("%d\n",strcmp(word,argv[2]));
                if (strcmp(word,argv[2]) == 0)
                {
                    printf("lines = %d\n", line);
                    close(fd);
                    free(lineBuf);
                    return 0;
                }
                word =strtok(NULL,p);
            }
             line++;
            lenline = 0;
        }
        lineBuf[lenline++] = ch;
        if (32 <lenline)
        {
            lineBuf = (char *)realloc(lineBuf,lenline*2*sizeof(char));
        }
        
    }
    printf("not found\n");
    close(fd);
    return 0;
}
